<template>
    <div class="register-wrap">
      <div class="header clearfix">
        <el-col :span="6" :offset="2">
          <h1 class="title">屋主注册</h1>
        </el-col>
      </div>
      <div class="content">
        <div class="form-content">
          <el-upload
            class="avatar-uploader"
            action="/api/upload/up.do"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-form ref="form" class="form" status-icon :rules="rules" :model="user" label-width="100px" label-position="right">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="user.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="user.password" placeholder="请输入6-12位的密码"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="repassword">
              <el-input type="password" v-model="user.repassword" placeholder="请确认密码"></el-input>
            </el-form-item>
            <el-form-item label="身份证号码" prop="idNumber">
              <el-input v-model="user.idNumber" placeholder="请输入身份证号码"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="user.phone" placeholder="请输入手机号"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="user.email" placeholder="请输入邮箱"></el-input>
            </el-form-item>
            <el-button type="primary" class="btn_submit" @click="submitForm('form')">注册</el-button>
          </el-form>
          <el-row>
            <el-col :span="8" :offset="16">
              <router-link to="/login" class="link">已有账号，去登录>></router-link>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="footer">
        <div class="links">
          <a href="http://www.qq.com" class="link" target="_blank">腾讯</a>|
          <a href="https://www.baidu.com" class="link" target="_blank">百度</a>|
          <a href="https://www.taobao.com" class="link" target="_blank">淘宝</a>|
          <a href="https://www.zhihu.com" class="link" target="_blank">知乎</a>|
        </div>
        <p class="copyright">
          Copyright © 2017 happymmall.com All Right Reserved
        </p>
      </div>
    </div>
</template>

<script>
  import ElCol from 'element-ui/packages/col/src/col';
  export default {
    components: {ElCol},
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }
        let reg = /^[A-Za-z0-9]{6,16}$/;
        if (!reg.test(value)) {
          callback(new Error('请输入6-12位有效字符作为密码'));
        }
        if (this.user.repassword !== '') {
          this.$refs.form.validateField('repassword');
        }
        callback();
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.user.password) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      };
      return {
        imageUrl: '',
        user: {
          username: '',
          password: '',
          repassword: '',
          phone: '',
          idNumber: '',
          email: '',
          image: ''
        },
        rules: {
          username: [
            {
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            }
          ],
          password: [
            {
              validator: validatePass,
              trigger: 'blur'
            },
            {
              required: true,
              trigger: 'blur'
            }
          ],
          repassword: [
            {
              validator: validatePass2,
              trigger: 'blur'
            },
            {
              required: true,
              trigger: 'blur'
            }
          ],
          idNumber: [
            {
              required: true,
              message: '请输入身份证号码',
              trigger: 'blur'
            }
          ],
          phone: [
            {
              required: true,
              message: '请输入手机号码',
              trigger: 'blur'
            }
          ],
          email: [
            {
              required: true,
              message: '请输入email',
              trigger: 'blur'
            }
          ]
        }
      };
    },
    methods: {
      handleAvatarSuccess(response, res, file) {
        console.log(res);
        console.log(response);
        console.log(file);
        console.log(response.data);
        // file:///Users/wuyin/Desktop/img/ea22ca9d-34be-40a3-ac01-12d655a1708d.jpg
        this.imageUrl = '/api/image/' + response.data;
      },
//      beforeAvatarUpload(file) {
//        const isJPG = file.type === 'image/jpeg';
//        const isLt2M = file.size / 1024 / 1024 < 2;
//        if (!isJPG) {
//          this.$message.error('上传头像图片只能是 JPG 格式!');
//        }
//        if (!isLt2M) {
//          this.$message.error('上传头像图片大小不能超过2MB!');
//        }
//        return true;
//      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 注册
            this.register();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      register() {
        this.$http.post('/api/host/user/register.do', this.$qs.stringify({
          'username': this.user.username,
          'password': this.user.password,
          'idNumber': this.user.idNumber,
          'phone': this.user.phone,
          'email': this.user.email,
          'image': this.imageUrl
        }))
          .then((response) => {
            var data = response.data;
            if (data.status === 0) {
              this.$message({
                message: `${data.msg},将在1秒后跳转登录页面`,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.$router.push({path: '/login'});
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          })
          .catch((error) => {
            this.$message.error(error.msg);
          });
        // console.log(this.user);
        // this.$http.post('/api/host/user/register.do',this.)
      }
    }
  };
</script>

<style>
  #app{
    margin: 0;
  }
  .register-wrap {
    width: 100%;
  }
  .register-wrap .header{
    width: 100%;
    height: 80px;
    line-height: 80px;
  }
  .header .header-content{
    height: 80px;
    line-height: 80px;
    width: 980px;
    margin: 0 auto;
  }
  .register-wrap .content{
    width: 100%;
    background-color: #409EFF;
    border-top: 1px solid #d8d9d9;
    border-bottom: 1px solid #d8d9d9;
  }
  .form-content{
    width: 420px;
    background-color: #fff;
    margin: 40px auto;
    padding: 20px 0;
    border: 1px solid #faf4f4;
    border-radius: 2px;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 50%;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
  .form{
    padding: 20px 20px 10px 20px;
  }
  .btn_submit{
    width: 100%;
  }
  .footer{
    font-size: 12px;
  }
  .link{
    text-decoration: none;
    cursor: pointer;
    color: #999999;
    font-size: 12px;
  }
  .footer .links {
    text-align: center;
    color: #666;
    line-height: 30px;
  }
  .footer .links .link {
    padding: 0 10px;
  }
  .footer .copyright {
    text-align: center;
    color: #666;
    line-height: 30px;
  }
  .login.link:hover{
    color: #409EFF;
  }
</style>
